<template>
  <bpm-c-subtitle :title="getLanguage('title.CostShareSubTitle')"
                  :isCollapse="false">
    <bpm-d-grid :note="gridNote"
                v-loading="showLoading"
                head="split"
                ref="splitallgrid"
                :showArray="showArray"
                :showCheckbox="true"
                :gridInfo="ssc.reimburse.whBtReimburseCostshare"
                :gridTitleInfo="gridTitleInfo"
                :gridShowInfo="gridShowInfo"
                :gridEditInfo="gridEditInfo"
                :gridWidthInfo="gridWidthInfo"
                :gridTypeInfo="gridTypeInfo"
                :pageMax="pageMax"
                :pageStart="pageStart"
                :pageCount="pageCount"
                :warnList="warnList"
                :writeAble="writeAble"
                :validateExp="validateExp"
                @gridStatusChanged="gridStatusChanged"
                @gridPageChanged="gridPageChanged"
                @gridClicked="gridClicked"
                @deleteClicked="deleteClicked"
                @gridDBLClicked="gridDBLClicked"
                @saveClicked="saveClicked"
                @gridCheckedChanged="gridCheckedChanged"
                @addClicked="addClicked"
                @gridCellEditEnd="gridCellEditEnd"
    />
  </bpm-c-subtitle>
</template>

<script>
import {mapState, mapActions} from 'vuex'

export default {
  name: 'bpmDSplitarea',
  data () {
    // let gridShowInfo = ['costcentername', 'internalordername', 'subjectname', 'notaxamount', 'tax', 'reimbursesum', 'actualreimbursesum', 'costcenterbudget']
    // let gridTitleInfo = ['成本中心', '内部订单', '核算科目', '不含税金额', '税额', '申请报销金额', '实际报销金额', '可用预算']
    // 保证总和为980，如果有复选框则总和为940
    // let gridWidthInfo = [150, 150, 140, 125, 125, 125, 125, 125]
    // let showArray = ['add', 'delete']
    return {
      validateExp: {
        3: {
          flag: true,
          maxlength: 14,
          require: true,
          positiveDecimal: true
        },
        4: {
          flag: true,
          maxlength: 14,
          positiveDecimal: true
        }
      },
      gridStatus: '',
      gridEditItem: {},
      gridLoading: false
      // gridTitleInfo: gridTitleInfo,
      // gridShowInfo: gridShowInfo,
      // gridWidthInfo: gridWidthInfo,
      // gridTypeInfo: ['text', 'text', 'text', 'number', 'number', 'number', 'number']
    }
  },
  computed: {
    ...mapState(['ssc', 'ssc.reimburse', 'pageTypeEnum', 'pageType', 'dialogloading']),
    ...mapState(['viewTableVisible', 'gridPageInfo', 'lang', 'otherI18n']),
    showLoading () {
      return this.gridLoading || (this.dialogloading !== 0)
    },
    gridNote () {
      if (this.pageType === this.pageTypeEnum.draft) {
        return this.otherI18n.dbclkNote
      }
      if (this.pageType === this.pageTypeEnum.approveFee) {
        return ''
      }
      return ''
    }, // this.getLanguage('alert.sjxzxgjl'),
    pageMax () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseCostshare.max
    },
    pageStart () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseCostshare.start
    },
    pageCount () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseCostshare.count
    },
    gridInfo () {
      return this.ssc.reimburse.whBtReimburseCostshare
    },
    writeAble () {
      if (this.pageType === this.pageTypeEnum.view || this.pageType === this.pageTypeEnum.approve) {
        return false
      }
      return true
    },
    showArray () {
      if (this.pageType === this.pageTypeEnum.draft) {
        return ['add', 'delete']
      }
      if (this.pageType === this.pageTypeEnum.approveFee) {
        return []
      }
      return []
    },
    gridShowInfo () {
      if (this.viewTableVisible.costCenterBudget === true) {
        return ['costcentername', 'internalordername', 'subjectname', 'notaxamount', 'tax', 'reimbursesum', 'actualreimbursesum', 'costcenterbudget']
      } else {
        return ['costcentername', 'internalordername', 'subjectname', 'notaxamount', 'tax', 'reimbursesum', 'actualreimbursesum']
      }
    },
    gridEditInfo () {
      if (this.pageType === this.pageTypeEnum.approveFee) {
        return [
          'notaxamount',
          'tax'
        ]
      }
      return []
    },
    gridTitleInfo () {
      if (this.viewTableVisible.costCenterBudget === true) {
        return [
          this.getLanguage('label.whBtReimburseCostshare.costcentername'),
          this.getLanguage('label.whBtReimburseCostshare.internalordername'),
          this.getLanguage('label.whBtReimburseCostshare.subjectname'),
          this.getLanguage('label.whBtReimburseCostshare.notaxamountCurrency'),
          this.getLanguage('label.whBtReimburseCostshare.tax'),
          this.getLanguage('label.whBtReimburseCostshare.reimbursesum'),
          this.getLanguage('label.whBtReimburseCostshare.actualreimbursesumCurrency'),
          this.getLanguage('label.whBtReimburseCostshare.costcenterbudget')
        ]
      } else {
        return [
          this.getLanguage('label.whBtReimburseCostshare.costcentername'),
          this.getLanguage('label.whBtReimburseCostshare.internalordername'),
          this.getLanguage('label.whBtReimburseCostshare.subjectname'),
          this.getLanguage('label.whBtReimburseCostshare.notaxamountCurrency'),
          this.getLanguage('label.whBtReimburseCostshare.tax'),
          this.getLanguage('label.whBtReimburseCostshare.reimbursesum'),
          this.getLanguage('label.whBtReimburseCostshare.actualreimbursesumCurrency')]
      }
    },
    gridWidthInfo () {
      // 保证总和为980，如果有复选框则总和为940
      if (this.viewTableVisible.costCenterBudget === true) {
        return [145, 150, 140, 100, 100, 100, 100, 100]
      } else {
        return [150, 150, 140, 125, 125, 125, 125]
      }
    },
    gridTypeInfo () {
      if (this.viewTableVisible.costCenterBudget === true) {
        return ['text', 'text', 'text', 'number', 'number', 'number', 'number', 'number']
      } else {
        return ['text', 'text', 'text', 'number', 'number', 'number', 'number']
      }
    },
    // 超预算标红
    warnList () {
      let warnListArray = []
      console.log('warnList failMids', this.$store.state.failMids)
      console.log('warnList gridShowInfo', this.$store.state.ssc.reimburse.whBtReimburseCostshare)
      if (this.$store.state.failMids && this.$store.state.ssc.reimburse.whBtReimburseCostshare.length > 0) {
        let failMidList = this.$store.state.failMids.split(',')
        if (failMidList.length > 0) {
          let midTemp = {}
          for (let j = 0; j < failMidList.length; j++) {
            for (let i = 0; i < this.$store.state.ssc.reimburse.whBtReimburseCostshare.length; i++) {
              midTemp = this.$store.state.ssc.reimburse.whBtReimburseCostshare[i].mid
              if (failMidList[j] === midTemp) {
                warnListArray.push(i)
              }
            }
          }
        }
      }
      console.log('warnList warnListArray ', warnListArray)
      return warnListArray
    }
  },
  methods: {
    ...mapActions(['delTableWh_bt_reimburse_costshare', 'getDataFromTableCostshare', 'setGridSelectMid', 'saveTableWh_bt_reimburse_costshare',
      'queryBudgetSubject', 'queryInternalOrder', 'calCostShareActSum', 'approveFeeAddCertMaking', 'getDataFromTableRcmDetails', 'getDataSscReimburseFromMoreTable',
      'querySplitInternalOrder']),
    gridClicked (index, item) {
      this.gridSelectIndex = index
      this.$store.state.gridSelectIndex.whBtReimburseCostshare = index
      this.setGridSelectMid(item.mid)
    },
    gridDBLClicked (index, item) {
      this.gridEditItem = item
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare = item
      console.log('费用分摊list的form首次取值gridEditItem', this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare)

      if (this.pageType !== this.pageTypeEnum.draft) { return }

      this.gridLoading = true
      setTimeout(() => {
        // let costcenterCode = this.ssc.reimburse.whBtReimburse.reimbursecostcentercode
        let costcenterCode = item.costcentercode
        // 获取内部订单数组
        let param = {}
        param.me = this
        param.queryInternalOrderCode = ''
        param.queryInternalOrderName = ''
        param.companyCode = this.ssc.reimburse.whBtReimburse.reimbursecompanycode
        param.costcenterCode = costcenterCode
        // this.queryInternalOrder(param)
        this.querySplitInternalOrder(param)
        if (item && item.internalordercode) {
          let param2 = {}
          param2.me = this
          param2.companyCode = this.ssc.reimburse.whBtReimburse.reimbursecompanycode
          param2.costingCenterCode = this.ssc.reimburse.whBtReimburse.reimbursecostcentercode
          console.log('3333333333333333333费用分摊行选中', item)
          param2.inwardOrderCode = item.internalordercode
          param2.budgetSubjectName = ''
          param2.budgetSubjectCode = ''
          param2.flag = 'split'
          this.queryBudgetSubject(param2)
        }
        this.gridLoading = false
      }, 100)
    },
    gridCheckedChanged (v) {
      let strs = ''
      console.log('vvvvvv=', v)
      for (var i = 0; i < v.length; i++) {
        strs += this.$store.state.ssc.reimburse.whBtReimburseCostshare[v[i]].mid + ','
        if (this.pageType === this.pageTypeEnum.approveFee) {
          this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare = Object.assign({}, this.$store.state.ssc.reimburse.whBtReimburseCostshare[v[i]])
        }
      }
      if (strs !== '') {
        strs = strs.substring(0, strs.length - 1)
      }
      this.$store.state.fyftCheckedMids = strs
    },
    gridStatusChanged (v) {
      this.gridStatus = v
      this.$store.state.fyftclfGradState = v
    },
    saveClicked () {
      if (this.pageType === this.pageTypeEnum.draft) {
        let saveFlag = 'update'
        if (this.gridStatus === 'add') {
          saveFlag = 'insert'
        }
        let innerOrder = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordercode
        let subjectCode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectcode

        if (innerOrder == null || innerOrder === '' || typeof innerOrder === 'undefined') {
          this.alert(this.getResource('alert.information.nbddbnwk'))
          return false
        }
        if (subjectCode == null || subjectCode === '' || typeof subjectCode === 'undefined') {
          this.alert(this.getResource('alert.information.kmbnwk'))
          return false
        }
        let tax = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.tax
        if (tax === null || tax === '' || tax === 0) {
          this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.tax = '0'
        }
        this.saveTableWh_bt_reimburse_costshare({
          saveFlag: saveFlag,
          me: this,
          data: this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare
        })
        // 保存后动作
        this.getDataFromTableCostshare({me: this, async: true})
        this.calCostShareActSum({
          me: this,
          data: this.$store.state.tsId,
          async: true
        })
      } else {
        if (this.isMakeCert() === '-1') {
          this.alert(this.getResource('alert.information.yjzzbyxxg')) // 已经制证不允许修改
          return false
        }
        if (this.approveFeeNoTaxAmountOnblur() === false) {
          return false
        }
        if (this.approveFeeTaxOnblur() === false) {
          return false
        }
        let saveFlag = 'update'
        let innerOrder = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordercode
        let subjectCode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectcode

        if (innerOrder == null || innerOrder === '' || typeof innerOrder === 'undefined') {
          this.alert(this.getResource('alert.information.nbddbnwk'))
          return false
        }
        if (subjectCode == null || subjectCode === '' || typeof subjectCode === 'undefined') {
          this.alert(this.getResource('alert.information.kmbnwk'))
          return false
        }
        let tax = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.tax
        if (tax === null || tax === '' || tax === 0) {
          this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.tax = '0'
        }
        this.saveTableWh_bt_reimburse_costshare({
          saveFlag: saveFlag,
          me: this,
          data: this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare
        })
        // 重新生成制证明细表
        this.approveFeeAddCertMaking({
          me: this,
          data: this.$store.state.tsId,
          async: false
        })
        // 保存后动作,查询动作已在保存后同步执行。
        // this.getDataFromTableCostshare({me: this, async: true})
        // // 统计费用分摊表金额
        this.calCostShareActSum({
          me: this,
          data: this.$store.state.tsId,
          async: true
        })
        // let paramJson = {}
        // paramJson.me = this
        // // paramJson.tableNames = 'wh_bt_reimburse,wh_bt_reimburse_dd_sub,wh_bt_reimburse_costshare,wh_bt_travelsubsidydetails,wh_bt_transportationdetails,wh_bt_faredetails,wh_bt_staydetails,wh_bt_reimburse_rcm_details,wh_bt_reimburse_rcm_sub'
        // paramJson.tableNames = 'WH_VBT_REIMBURSE_RCM_DETAILS'
        // paramJson.BB = 'N'
        // this.getDataSscReimburseFromMoreTable(paramJson)
        this.getDataFromTableRcmDetails({me: this, async: true})
      }
      this.$refs.splitallgrid.hideGridHead(true)
      return true
    },
    addClicked () {
      console.log('----------ssss-----')
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.tsId = this.$store.state.tsId
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.mid = 'MID-' + this.getUuid()
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.costcentercode = this.$store.state.ssc.reimburse.whBtReimburse.reimbursecostcentercode
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.costcentername = this.$store.state.ssc.reimburse.whBtReimburse.reimbursecompanycostcenter
      // this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordercode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseDdSub.internalordercode
      // this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordername = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseDdSub.internalordername
      // this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectcode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseDdSub.subjectcode
      // this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectname = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseDdSub.subjectname
      if (this.pageType !== this.pageTypeEnum.draft) { return }
      console.log('----------dd-d-d-d-d-')
      let costcenterCode = this.ssc.reimburse.whBtReimburse.reimbursecostcentercode
      if (!costcenterCode || costcenterCode.length <= 0) {
        // 清空内部订单和核算科目数组
        this.$store.commit('updateDataSscReimburse', { name: 'internalOrderList', value: [] })
      } else {
        // 获取内部订单数组
        let param = {}
        param.me = this
        param.queryInternalOrderCode = ''
        param.queryInternalOrderName = ''
        param.companyCode = this.ssc.reimburse.whBtReimburse.reimbursecompanycode
        param.costcenterCode = costcenterCode
        // this.queryInternalOrder(param)
        this.querySplitInternalOrder(param)
      }
    },
    deleteClicked () {
      console.log('2323232')
      if (this.pageType === this.pageTypeEnum.draft) {
        this.httpBpmDelBz({mids: this.$store.state.fyftCheckedMids.split(','), tableName: 'wh_bt_reimburse_costshare'})
        this.calCostShareActSum({
          me: this,
          data: this.$store.state.tsId,
          async: true
        })
        this.getDataFromTableCostshare({me: this, async: true})
      }
    },
    gridPageChanged (v) {
      console.log('split', v)
      if (v.name === 'count' || v.name === 'start') {
        let json = {
          path: 'whBtReimburseCostshare',
          name: v.name,
          value: v.value
        }
        this.$store.commit('upGridPageInfoWithPathAndValue', json)
        this.getDataFromTableCostshare({ me: this, async: true})
      }
    },
    gridCellEditEnd: function (record, recordFieldValue) {
      this.gridLoading = true
      setTimeout(() => {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare = record
        if (!this.saveClicked()) {
          this.getDataFromTableCostshare({me: this, async: true})
        } else {
          this.tips(this.getResource('alert.tips.sjxgcg'))
        }
        this.gridLoading = false
      }, 100)
    }
  }// ,
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
